package algo.sort;

public class Up2DownMergeSort <T extends Comparable<? super T>> extends MergeSort<T>{
    @Override
    public void sort(T[] nums) {
        aux = (T[]) new Comparable[nums.length];
        toSort(nums, 0, nums.length - 1);
    }

    private void toSort(T[] nums, int left, int right) {
        if (right <= left) {
            return;
        }

        int mid = left + (right - left)/2;
        toSort(nums, left, mid);
        toSort(nums, mid + 1, right);
        merge(nums, left, mid, right);
    }

    private void toSortDesc(T[] nums, int left, int right) {
        if (right <= left) {
            return;
        }

        int mid = left + (right - left)/2;
        toSortDesc(nums, left, mid);
        toSortDesc(nums, mid + 1, right);
        mergeDesc(nums, left, mid, right);
    }

    @Override
    public void sortDesc(T[] nums) {
        aux = (T[]) new Comparable[nums.length];
        toSortDesc(nums, 0, nums.length - 1);
    }
}
